package com.itlong.cloud.intercept.sdkV3;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.sdk.CloudTalkSDKAppUserVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.ecode.CloudTalkErrorCodeEnum;
import com.itlong.cloud.user.sdk.ICloudTalkSDKAppUserServiceClient;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      云对讲SDK 请求身份验证拦截器
 * </desc>
 *
 * @createDate 2018/05/16
 */
@Aspect
@Component
public class SDKCloudTalkAppLoginIntercept {
    private static Logger LOG = LoggerFactory.getLogger(SDKCloudTalkAppLoginIntercept.class);

    @Value("${"+ PlatformConstants.CLOUD_TALK_SDK_APP_USER_TOKEN_DONTINTERCEPT+"}")
    String dontIntercept;   //不需要拦截的方法名

    @Autowired
    ICloudTalkSDKAppUserServiceClient iCloudTalkSDKAppUserServiceClient;

    // 定义一个切入点
    @Pointcut("execution(* com.itlong.cloud.controller.sdkV3.intercom.server.SDKCloudTalkUserController.*(..))")
    public void excudeService(){}

    /**
     * <desc>
     *     云对讲SDK APP身份验证拦截器
     * </desc>
     *
     * @param thisJoinPoint
     * @return
     * @author Jiaqi.X
     * @createDate 2018/05/16
     */
    //@Around("excudeService()")
    public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint) throws Throwable{
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String[] interceptMethod= StringHandlerUtil.splitString(dontIntercept);
        String uri = request.getRequestURI();
        if (uri.indexOf("error") > -1 || uri.indexOf("404") > -1) {   //屏蔽错误中继续错误的死循环
            return thisJoinPoint.proceed();
        }
        //获取拦截方法的所有属性
        MethodSignature signature = (MethodSignature) thisJoinPoint.getSignature();
        //循环配置不需要拦截的方法
        for(String method:interceptMethod){
            if(signature.getMethod().getName().equals(method)){
                return thisJoinPoint.proceed();
            }
        }
        //获取token
        String token=request.getParameter("token");
        if(StringUtils.isNotBlank(token)){
            //解密token拿到用户信息
            CloudTalkSDKAppUserVO vo = iCloudTalkSDKAppUserServiceClient.retrieve(token);
            if(vo==null){
                return new MessageVO(CloudTalkErrorCodeEnum.USER_TOKEN_ERROR.getErrorCode());
            }
            return thisJoinPoint.proceed();
        }else{
            return new MessageVO(CloudTalkErrorCodeEnum.USER_TOKEN_ERROR.getErrorCode());
        }
    }
}
